package com.javaweb.blogcite.controller;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.pojo.About;
import com.javaweb.blogcite.service.AboutService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**获取url*/
    @RestController
    public class AboutController {
    @Resource
    private AboutService aboutService;

    @GetMapping("/api/about")
    public ResponseResult<String> getAbout() {
        About abouts = aboutService.listAllAbout();
        String url= abouts.getUrl();
        ResponseResult<String> result = new ResponseResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(url);

        return result;
    }
    /**设置url*/
    @JwtCheck
    @PutMapping("/api/about")
    public ResponseResult<String> setAbout(@RequestBody String url) {
        String jsonString =url;

        // 将 JSON 字符串转换为 JsonObject 对象
        JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();

        // 提取 url 字段的值
        String url1 = jsonObject.get("url").getAsString();
        boolean success = aboutService.updateAbout(url1);
        ResponseResult result = new ResponseResult();

        if (success) {
            result.setCode(0);
            result.setMsg("success");
            result.setData(url1);
        } else {
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }
}
/**
 * @author cbhenbo
 * @date 2020/11/23-10:36
 */